﻿using JzSport.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JzSport.Api
{
    class GetDefaultActivityListAction : GetAction<Params.GetDefaultActivityListParams,
        Results.GetDefaultActivityListResult>
    {
        protected override ActionResult<Results.GetDefaultActivityListResult> OnExecute(ActionContext context, SysParameters sysParameters,
            Params.GetDefaultActivityListParams opParams)
        {
            JizSport.Common.Logger.DebugToTag(LogTag.TAG, "GetDefaultActivityListAction city:{0},longitude:{1},latitude:{2},page:{3}", opParams.City,
                opParams.Longitude,
                opParams.Latitude,
                opParams.Page);

            int limit = 10;
            int start = opParams.Page * limit;
            ServiceResult<PagedList<Activity>> serviceResult = new ActivityService().GetDefaultActivityList(opParams.City,
                opParams.Longitude,
                opParams.Latitude,
                start,
                limit);

            Results.GetDefaultActivityListResult actionResult = new Results.GetDefaultActivityListResult();

            if (serviceResult.ErrorCode == ErrorCode.NoError)
            {
                actionResult.Start = start;
                actionResult.Limit = limit;
                actionResult.Page = opParams.Page;
                actionResult.Total = serviceResult.Data.Total;

                for (int index = 0; index < serviceResult.Data.Items.Count; index++)
                {
                    Activity activity = serviceResult.Data.Items[index];

                    Results.GetDefaultActivityListResult.ActivityItem item = new Results.GetDefaultActivityListResult.ActivityItem();

                    item.Id = activity.Id;
                    item.Title = activity.Title;
                    item.CategoryCode = activity.CategoryCode;
                    item.Longitude = activity.Longitude;
                    item.Latitude = activity.Latitude;
                    item.MaxPersonCount = activity.MaxPersonCount;
                    item.CurrentPersonCount = activity.CurrentPersonCount;
                    item.BeginTime = activity.BeginTime;
                    item.EndTime = activity.EndTime;
                    item.CreatedTime = activity.CreatedTime;
                    item.IsFinished = activity.IsFinished;
                    item.Address = activity.Address;
                    item.Distance = Util.GetDistance(opParams.Latitude, opParams.Longitude, item.Latitude, item.Longitude);

                    actionResult.Items.Add(item);
                }

                if (actionResult.Start + actionResult.Items.Count >= actionResult.Total)
                {
                    actionResult.IsTheEnd = true;
                }
                else
                {
                    actionResult.IsTheEnd = false;
                }

                return new ActionResult<Results.GetDefaultActivityListResult>("1.0.3", true, serviceResult.ErrorCode,
                    ErrorCode.ToString(serviceResult.ErrorCode), actionResult);
            }
            return new ActionResult<Results.GetDefaultActivityListResult>("1.0.3", false, serviceResult.ErrorCode,
                    ErrorCode.ToString(serviceResult.ErrorCode), actionResult);
        }
    }
}
